package by.grsu.epam.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import by.grsu.epam.domain.account.Account;
import by.grsu.epam.domain.account.PayCard;
import by.grsu.epam.domain.service.Service;
import by.grsu.epam.domain.user.impl.Subscriber;
import by.grsu.epam.service.entitiesservice.AccountService;
import by.grsu.epam.service.entitiesservice.AdministratorService;
import by.grsu.epam.service.entitiesservice.PayCardService;
import by.grsu.epam.service.entitiesservice.ServiceService;
import by.grsu.epam.service.entitiesservice.SubscriberService;

@Controller
public class AdministratorController {

	@Autowired
	private AdministratorService administratorService;

	@Autowired
	private PayCardService payCardService;
	
	@Autowired
	private ServiceService serviceService;
	
	@Autowired
	private SubscriberService subscriberService;
	
	@Autowired
	private AccountService accountService;

	@RequestMapping(value = "/administrator/home", method = RequestMethod.GET)
	public ModelAndView home() {
		ModelAndView mav = new ModelAndView("/administrator/home");

		return mav;
	}

	// Create Pay Card
	@RequestMapping(value = "/administrator/payCards/create", method = RequestMethod.GET)
	public ModelAndView newPayCard() {
		ModelAndView mav = new ModelAndView("/administrator/payCards/create");

		mav.addObject("payCard", new PayCard());

		return mav;
	}

	@RequestMapping(value = "/administrator/payCards/create", method = RequestMethod.POST)
	public ModelAndView createPayCard(@ModelAttribute(value = "payCard") PayCard payCard, BindingResult result) {
		ModelAndView mav = new ModelAndView();

		if (result.hasErrors()) {
			mav.setViewName("redirect:/administrator/payCards/create");
		} else {
			mav.setViewName("redirect:/administrator/payCards/getList");

			payCardService.save(payCard);
		}

		return mav;
	}

	// Get all Pay Cards
	@RequestMapping(value = "/administrator/payCards/getList", method = RequestMethod.GET)
	public ModelAndView getListPayCards() {
		ModelAndView mav = new ModelAndView("/administrator/payCards/getList");

		List<PayCard> pl = payCardService.findAll();

		mav.addObject("payCards", pl);

		return mav;
	}
	
	// Create Sevice
	@RequestMapping(value = "/administrator/services/create", method = RequestMethod.GET)
	public ModelAndView newService() {
		ModelAndView mav = new ModelAndView("/administrator/services/create");

		mav.addObject("service", new Service());

		return mav;
	}

	@RequestMapping(value = "/administrator/services/create", method = RequestMethod.POST)
	public ModelAndView createService(@ModelAttribute(value = "service") Service service, BindingResult result) {
		ModelAndView mav = new ModelAndView();

		if (result.hasErrors()) {
			mav.setViewName("redirect:/administrator/services/create");
		} else {
			mav.setViewName("redirect:/administrator/services/getList");

			serviceService.save(service);
		}

		return mav;
	}

	// Get all Services
	@RequestMapping(value = "/administrator/services/getList", method = RequestMethod.GET)
	public ModelAndView getListServices() {
		ModelAndView mav = new ModelAndView("/administrator/services/getList");

		List<Service> services = serviceService.findAll();

		mav.addObject("services", services);

		return mav;
	}
	
	// Remove Service
	@RequestMapping(value = "/administrator/services/remove/{serviceId}", method = RequestMethod.GET)
	public ModelAndView removeService(@PathVariable Long serviceId){
		ModelAndView mav = new ModelAndView("redirect:/administrator/services/getList");
		serviceService.delete(serviceId);
		
		return mav;
	}
	
	// Get List Subscribers
	@RequestMapping(value = "/administrator/subscribers/getList", method = RequestMethod.GET)
	public ModelAndView getListSubscribers(){
		ModelAndView mav = new ModelAndView("/administrator/subscribers/getList");
		mav.addObject("subscribers", subscriberService.findAll());
		
		return mav;
	}
	
	// Get User Info
	@RequestMapping(value = "/administrator/subscribers/{subscriberId}", method = RequestMethod.GET)
	public ModelAndView getSubscriberInfo(@PathVariable Long subscriberId, HttpSession session){
		ModelAndView mav = new ModelAndView("/administrator/subscribers/subscriberId");
		session.setAttribute("subscriberId", subscriberId);
		
		Subscriber subscriber = subscriberService.findById(subscriberId);
		List<Account> accounts = accountService.findBySubscriberUsername(subscriber.getUsername());
		
		mav.addObject("subscriber", subscriber);
		mav.addObject("subscriberAccounts", accounts);
		
		return mav;
	}
	
	
	// Block Account
	@RequestMapping(value = "/administrator/subscribers/{accountId}/block", method = RequestMethod.GET)
	public ModelAndView blockAccount(@PathVariable Long accountId, HttpSession session){
		Long subscriberId = (Long) session.getAttribute("subscriberId");
		ModelAndView mav = new ModelAndView("redirect:/administrator/subscribers/" + subscriberId.toString());
		
		Account account = accountService.findById(accountId);
		account.setAccountIsNotLocked(false);
		accountService.update(account);
		
		return mav;
	}
	
}
